package com.jic.product.controller.manage;

import com.jic.common.base.vo.Page;
import com.jic.common.base.vo.PageResult;
import com.jic.common.base.vo.RestResult;
import com.jic.product.common.util.BeanUtil;
import com.jic.product.entity.StoreInventory;
import com.jic.product.manage.request.ProductBatchUpdateRequest;
import com.jic.product.manage.request.StoreInventoryRequest;
import com.jic.product.manage.response.StoreInventoryResponse;
import com.jic.product.service.StoreInventoryService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.stream.Collectors;

/**
 * Created by HeJun on 2020/1/18
 *
 * @Description:
 */
@Api(value="店铺库存管理controller",tags = "店铺库存管理", description = "StoreInventoryController")
@RestController
@RequestMapping("/storeInventoryManagement")
public class StoreInventoryController {

    @Autowired
    StoreInventoryService storeInventoryService;

    @ApiOperation(value = "获取店铺库存信息", notes = "获取店铺库存信息")
    @PostMapping("/queryPage")
    public RestResult<PageResult<StoreInventoryResponse>> queryPage(@RequestBody StoreInventoryRequest form) {
        Page page = new Page();
        page.setLimit(form.getLimit());
        page.setStart(form.getStart());

        PageResult<StoreInventoryResponse> result = storeInventoryService.queryPage(form , page);

        List<StoreInventoryResponse> list = result.getRows().stream().map(temp -> {
            StoreInventoryResponse bmr = BeanUtil.copy(temp,StoreInventoryResponse.class);
            return bmr;
        }).filter(item -> item != null).collect(Collectors.toList());
        PageResult<StoreInventoryResponse> pageResult = new PageResult<>();

        pageResult.setRows(list);
        pageResult.setTotal(result.getTotal());
        return RestResult.success(pageResult);
    }

    @ApiOperation(value="新增店铺库存", notes="新增店铺库存")
    @PostMapping("/insert")
    public RestResult<Long> insert(@RequestBody StoreInventoryRequest t) {
        long result = 0;
        try {
            StoreInventory wh = BeanUtil.copy(t, StoreInventory.class);
            result = storeInventoryService.insert(wh);
        } catch (Exception e) {
            e.printStackTrace();
            return RestResult.error("9999", e.getLocalizedMessage());
        }
        return RestResult.success(result);
    }

    @ApiOperation(value="修改店铺库存", notes="修改店铺库存")
    @PostMapping("/update")
    public RestResult<Long> update(@RequestBody StoreInventoryRequest t) {
        long result = 0;
        try {
            StoreInventory wh = BeanUtil.copy(t, StoreInventory.class);
            result = storeInventoryService.update(wh);
        } catch (Exception e) {
            e.printStackTrace();
            return RestResult.error("9999", e.getLocalizedMessage());
        }
        return RestResult.success(result);
    }

    @ApiOperation(value="批量修改修改店铺库存", notes="批量修改修改店铺库存")
    @PostMapping("/batchUpdateProductStock")
    public RestResult<Long> batchUpdateProductStock(@RequestBody ProductBatchUpdateRequest productBatchUpdateRequest) {
        return storeInventoryService.batchUpdateProductStock(productBatchUpdateRequest);
    }
    @ApiOperation(value="每日可用库存更新", notes="每日可用库存更新")
    @PostMapping("/batchUpdateAvailableStock")
    public RestResult<Long> batchUpdateAvailableStock() {
        return storeInventoryService.batchUpdateAvailableStock();
    }


}
